home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 18 / CU Amiga Magazine's Super CD-ROM 18 (1997)(EMAP Images)(GB)[!][issue 1998-01].iso / CUCD / Online / hsc / source / hsclib / input.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-11-02  |  4.9 KB  |  238 lines

  1. /*
  2.  * This source code is part of hsc, a html-preprocessor,
  3.  * Copyright (C) 1995-1997  Thomas Aglassinger
  4.  *
  5.  * This program is free software; you can redistribute it and/or modify
  6.  * it under the terms of the GNU General Public License as published by
  7.  * the Free Software Foundation; either version 2 of the License, or
  8.  * (at your option) any later version.
  9.  *
  10.  * This program is distributed in the hope that it will be useful,
  11.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.  * GNU General Public License for more details.
  14.  *
  15.  * You should have received a copy of the GNU General Public License
  16.  * along with this program; if not, write to the Free Software
  17.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  *
  19.  */
  20. /*
  21.  * hsclib/input.c
  22.  *
  23.  * basic functions for parsing input
  24.  *
  25.  * updated: 16-May-1997
  26.  * created: 29-Jul-1995
  27.  */
  28.  
  29. #include <ctype.h>
  30.  
  31. #include "hsclib/inc_base.h"
  32.  
  33. /*
  34.  *---------------------------
  35.  * overloaded methods for
  36.  * INFILE class
  37.  *---------------------------
  38.  */
  39.  
  40. /*
  41.  * hsc_whtspc
  42.  *
  43.  * decides if an char is a white-space
  44.  *
  45.  * params: ch...char to test
  46.  * result: TRUE, if ch is a 'normal' ch
  47.  *
  48.  * NOTE: this function is used as is_ws-methode
  49.  *       for the INFILE class
  50.  */
  51. BOOL hsc_whtspc(int ch)
  52. {
  53.     if ((ch == ' ')
  54.         || (ch == '\n')
  55. #if (!defined MSDOS)  /* HSC_INTO */
  56.         || (ch == '\r')
  57. #endif
  58.         || (ch == '\t')
  59.         )
  60.     {
  61.         return TRUE;
  62.     }
  63.     else
  64.         return FALSE;
  65. }
  66.  
  67. /*
  68.  * hsc_normch
  69.  *
  70.  * decides if an char is an 'normal' char
  71.  *
  72.  * params: ch...char to test
  73.  * result: TRUE, if ch is a 'normal' ch
  74.  *
  75.  * NOTE: this function is used as is_nc-methode
  76.  *       for the INFILE class
  77.  */
  78. BOOL hsc_normch(int ch)
  79. {
  80.     if (((ch >= '0') && (ch <= '9'))
  81.         || ((ch >= 'a') && (ch <= 'z'))
  82.         || ((ch >= 'A') && (ch <= 'Z'))
  83.         || (ch == '_') || (ch == '-') || (ch == '.')
  84.         )
  85.     {
  86.         return TRUE;
  87.     }
  88.     else
  89.         return FALSE;
  90. }
  91.  
  92. /*
  93.  * hsc_normch_tagid
  94.  *
  95.  * decides if an char is an 'normal' char for tagnames
  96.  *
  97.  * params: ch...char to test
  98.  * result: TRUE, if ch is a 'normal' ch
  99.  *
  100.  * NOTE: this function is used as is_nc-methode
  101.  *       for the infile class
  102.  */
  103. BOOL hsc_normch_tagid(int ch)
  104. {
  105.     BOOL found = hsc_normch(ch);
  106.  
  107.     if (!found)
  108.         if (strchr(HSC_TAGID, ch))
  109.             found = TRUE;
  110.  
  111.     return (found);
  112. }
  113.  
  114. /*
  115.  *-------------------------------------
  116.  * read a tag/attribute name from input file
  117.  *-------------------------------------
  118.  */
  119.  
  120. /*
  121.  * infget_tagid
  122.  *
  123.  * read next word from input, but with a
  124.  * different is_nc-methode that also handles
  125.  * the id for hsc-tags (usually "$")
  126.  */
  127. STRPTR infget_tagid(HSCPRC * hp)
  128. {
  129.     INFILE *inpf = hp->inpf;
  130.     STRPTR tagid = NULL;
  131.  
  132.     BOOL(*old_is_nc) (int ch);
  133.  
  134.     old_is_nc = inpf->is_nc;    /* remember old is_nc-methode */
  135.     inpf->is_nc = hsc_normch_tagid;
  136.     tagid = infgetw(inpf);      /* read tagid */
  137.     if (!tagid)
  138.         hsc_msg_eof(hp, "reading tag name");
  139.     inpf->is_nc = old_is_nc;    /* remember old is_nc-methode */
  140.  
  141.     return (tagid);
  142. }
  143.  
  144. /*
  145.  * infget_attrid
  146.  *
  147.  * read next word from input, but with a
  148.  * different is_nc-methode that also handles
  149.  * the id for hsc-attribs (usually "$")
  150.  */
  151. STRPTR infget_attrid(HSCPRC * hp)
  152. {
  153.     INFILE *inpf = hp->inpf;
  154.     STRPTR attrid = NULL;
  155.  
  156.     BOOL(*old_is_nc) (int ch);
  157.  
  158.     old_is_nc = inpf->is_nc;    /* remember old is_nc-methode */
  159.     inpf->is_nc = hsc_normch_tagid;
  160.     attrid = infgetw(inpf);     /* read attrid */
  161.     if (!attrid)
  162.         hsc_msg_eof(hp, "reading attribute name");
  163.     inpf->is_nc = old_is_nc;    /* remember old is_nc-methode */
  164.  
  165.     return (attrid);
  166. }
  167.  
  168. /*
  169.  *-------------------------------------
  170.  * parse simple chars/words
  171.  *-------------------------------------
  172.  */
  173.  
  174. /*
  175.  * parse_wd
  176.  *
  177.  * check if a expected word really occured and
  178.  * display error message if neccessary
  179.  *
  180.  * params: inpf.....input file to read char from
  181.  *         expstr...expected word
  182.  * result: TRUE if successful, FALSE if wrong char found
  183.  */
  184. BOOL parse_wd(HSCPRC * hp, STRPTR expstr)
  185. {
  186.     INFILE *inpf = hp->inpf;
  187.     BOOL value = TRUE;
  188.  
  189.     if (expstr)
  190.     {
  191.         STRPTR nw = infgetw(inpf);
  192.  
  193.         /* check for expeted word */
  194.         if (!nw || upstrcmp(nw, expstr))
  195.         {
  196.             if (!nw)
  197.                 nw = "<EOF>";
  198.  
  199.             hsc_message(hp, MSG_UNEXPT_CH,
  200.                         "expected %q, found %q", expstr, nw);
  201.             value = FALSE;
  202.         }
  203.     }
  204.     else
  205.     {
  206.         panic("no data to expect");
  207.     }
  208.  
  209.     return (value);
  210. }
  211.  
  212. /*
  213.  * parse_eq
  214.  *
  215.  * check for '='
  216.  *
  217.  * params: inpf...input file to read char from
  218.  * result: -1 if successful, 0 if wrong char found
  219.  */
  220. BOOL parse_eq(HSCPRC * hp)
  221. {
  222.     return (parse_wd(hp, "="));
  223. }
  224.  
  225. /*
  226.  * parse_gt
  227.  *
  228.  * check for '>'
  229.  *
  230.  * params: inpf...input file to read char from
  231.  * result: -1 if successful, 0 if wrong char found
  232.  */
  233. BOOL parse_gt(HSCPRC * hp)
  234. {
  235.     return (parse_wd(hp, ">"));
  236. }
  237.  
  238.